package com.cdqidi.core.weixinmanager.controller.api;

import com.cdqidi.core.weixinmanager.service.SubscribeService;
import com.cdqidi.core.weixinmanager.service.TemplateMsgReportService;
import com.cdqidi.core.weixinmanager.service.wx.WxService;
import com.jfinal.springboot.weixin.annotation.ApiType;
import com.jfinal.springboot.weixin.annotation.WxApi;
import com.jfinal.springboot.weixin.jfinal.WxaMsgController;
import com.jfinal.wxaapp.WxaConfigKit;
import com.jfinal.wxaapp.msg.bean.WxaImageMsg;
import com.jfinal.wxaapp.msg.bean.WxaMediaCheckMsg;
import com.jfinal.wxaapp.msg.bean.WxaTextMsg;
import com.jfinal.wxaapp.msg.bean.WxaUserEnterSessionMsg;
import com.jfinal.wxaapp.msg.event.InSubscribeNoticesEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * APP_微信推送小程序消息
 *
 * @author sjk
 * @since 1.0.0
 */
@Controller
@WxApi(type = ApiType.WXA)
@RequestMapping("/wxmanager/wxaMsg")
@Slf4j
@RequiredArgsConstructor
public class WxaMsgInfoController extends WxaMsgController {
    private final WxService wxService;
    private static final String KEY_WORD = "1";
    private final TemplateMsgReportService templateMsgReportService;
    private final SubscribeService subscribeService;
//    private static final String SESSION_FROM_MSG = "打开公号二维码";


    @Override
    protected void processWxaMediaCheckMsg(WxaMediaCheckMsg wxaMsg) {
//        wxaMsgService.processWxaMediaCheckMsg(wxaMsg);
    }

    @Override
    protected void processTextMsg(WxaTextMsg textMsg) {
        String content = textMsg.getContent();
        if (KEY_WORD.equals(content)) {
            wxService.sendWxaMessage(WxaConfigKit.getAppId(), textMsg.getFromUserName());
        } else {
            log.info("err,keyWord: {}", content);
        }
    }

    @Override
    protected void processImageMsg(WxaImageMsg imageMsg) {

    }

    @Override
    protected void processUserEnterSessionMsg(WxaUserEnterSessionMsg userEnterSessionMsg) {
//        String sessionFrom = userEnterSessionMsg.getSessionFrom();
//        if(SESSION_FROM_MSG.equals(sessionFrom)){
//            wxService.sendWxaMessage(WxaConfigKit.getAppId(), userEnterSessionMsg.getFromUserName());
//        }else{
//            log.info("err,sessionFrom: "+sessionFrom);
//        }

    }


    @Override
    protected void processInSubscribeNoticesEvent(InSubscribeNoticesEvent inSubscribeNoticesEvent) {
        List<?> list = inSubscribeNoticesEvent.getList();
        if (null == list || list.isEmpty()) {
            return;
        }
        if (inSubscribeNoticesEvent.isEventByMsgSent()) {
            //模板消息推送状态
            templateMsgReportService.saveMsg(inSubscribeNoticesEvent.getFromUserName(), inSubscribeNoticesEvent.getCreateTime(), inSubscribeNoticesEvent.getList());
        } else {
            subscribeService.saveOrUpdate(inSubscribeNoticesEvent.getFromUserName(),inSubscribeNoticesEvent.getCreateTime(),inSubscribeNoticesEvent.getList());
        }
    }
}